package com.derbysoft.ota.application.service.impl;

import org.w3c.dom.Element;

import com.derbysoft.ota.application.service.HotelCodeRetriever;
import com.derbysoft.ota.application.translator.MetaDatas;
import com.derbysoft.ota.interfaces.protocol._2009a.OTACancelRQ;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.googlecode.functionalcollections.FunctionalIterables;

public class TpaExtensionHotelCodeRetriever implements HotelCodeRetriever {

	@Override
	public String retrieve(OTACancelRQ otaCancelRQ) {
		String errorMessage = "Please provide us  the " + MetaDatas.HOTEL_CODE_EXTENSION_KEY + " TPAExtensions in OTACancelRQ.";
		Element hotelCodeElement = FunctionalIterables.make(
			Preconditions.checkNotNull(
				otaCancelRQ.getTPAExtensions(),
				errorMessage
			).getAny())
		.detect(new Predicate<Element>() {
			public boolean apply(Element input) {
				return MetaDatas.HOTEL_CODE_EXTENSION_KEY.equals(input.getTagName());
			}
		});
		return Preconditions.checkNotNull(hotelCodeElement, errorMessage).getTextContent();
	}

}
